﻿var Calendar = function (e, t) {
    this.el = $(e), this.options = $.extend(!0, {}, this.options, t), this.init();
};

Calendar.prototype = {
    options: {
        calendar: ["日", "一", "二", "三", "四", "五", "六"],
        newDate: new Date()
    },
    init: function () {
        var e = this, t = e.el, a = e.options;
        e.holiDays = {}, e.focusDays = [], t.addClass("calendar-content"), e._createToolbar(),
            e._createCalendar(), $(document.body).on("click", ".month-prev", function () {
                e.prevMonth(a.newDate);
            }), $(document.body).on("click", ".month-next", function () {
                e.nextMonth(a.newDate);
            }), $(document.body).on("click", ".calendar-toolbar-year", function () {
                $(".dropdown-year").toggleClass("open");
                for (var e = a.newDate.getFullYear(), t = [], n = 0; n < 19; n++) 5 == n ? t[t.length] = '<li class="year-item active">' : t[t.length] = '<li class="year-item">',
                    t[t.length] = '<span class="year-check">' + (e - 5 + n) + "</span>", t[t.length] = "<span >年</span>",
                    t[t.length] = "</li>";
                document.getElementById("dropdown-year").innerHTML = t.join("");
            }), $(document.body).on("click", ".year-item", function (t) {
                var a = parseInt($(t.currentTarget).find(".year-check")[0].innerText), n = e._cloneDate(e.options.newDate);
                n.setFullYear(a), e.options.newDate = n, e.holiDays.date = e._refreshCalendar(n),
                    $(".toolbar-year")[0].innerHTML = n.getFullYear(), e.focusDays = [];
            }), $(document.body).on("click", ".calendar-toolbar-month", function () {
                $(".dropdown-month").toggleClass("open");
            }), $(document.body).on("click", ".month-item", function (t) {
                var a = parseInt($(t.currentTarget).find(".month-check")[0].innerText), n = e._cloneDate(e.options.newDate);
                n.setMonth(a - 1), e.options.newDate = n, e.holiDays.date = e._refreshCalendar(n),
                    $(".toolbar-month")[0].innerHTML = n.getMonth() + 1, e.focusDays = [];
            }), $(document.body).on("click", ".totoday", function () {
                e.options.newDate = e.newDate = new Date(), e.holiDays.date = e._refreshCalendar(e.newDate),
                    $(".toolbar-year")[0].innerHTML = e.newDate.getFullYear(), $(".toolbar-month")[0].innerHTML = e.newDate.getMonth() + 1;
            });
    },
    prevMonth: function (e) {
        var t = this;
        t.focusDays = [], t.newDate = new Date(e.setMonth(e.getMonth() - 1)), t.holiDays.date = t._refreshCalendar(t.newDate),
            $(".toolbar-year")[0].innerHTML = t.newDate.getFullYear(), $(".toolbar-month")[0].innerHTML = t.newDate.getMonth() + 1;
    },
    nextMonth: function (e) {
        var t = this;
        t.focusDays = [], t.newDate = new Date(e.setMonth(e.getMonth() + 1)), t.holiDays.date = t._refreshCalendar(t.newDate),
            $(".toolbar-year")[0].innerHTML = t.newDate.getFullYear(), $(".toolbar-month")[0].innerHTML = t.newDate.getMonth() + 1,
            t.focusDays = [];
    },
    _createToolbar: function () {
        var e = this, t = e.options.newDate, a = [];
        a[a.length] = '<div class="calendar-toolbar">', a[a.length] = '<div class="calendar-toolbar-centerbox">',
            a[a.length] = '<div class="month-prev"><a class="fa fa-angle-left"></a></div>',
            a[a.length] = '<div class="btnc-group">', a[a.length] = '<div class="btnc calendar-toolbar-year">',
            a[a.length] = '<span class="toolbar-year">' + t.getFullYear() + "</span>", a[a.length] = "<span>年</span>",
            a[a.length] = '<span class="fa fa-caret-down"></span>', a[a.length] = '<ul id="dropdown-year" class="dropdown-year">',
            a[a.length] = "</ul>", a[a.length] = "</div>", a[a.length] = '<div class="btnc calendar-toolbar-month">',
            a[a.length] = '<span class="toolbar-month">' + (t.getMonth() + 1) + "</span>", a[a.length] = "<span>月</span>",
            a[a.length] = '<span class="fa fa-caret-down"></span>', a[a.length] = '<ul class="dropdown-month">';
        for (var n = 1; 12 >= n; n++) a[a.length] = '<li class="month-item">', a[a.length] = '<span class="month-check">' + n + "</span>",
            a[a.length] = "<span >月</span>", a[a.length] = "</li>";
        a[a.length] = "</ul>", a[a.length] = "</div>", a[a.length] = "</div>", a[a.length] = '<div class="month-next"><a class="fa fa-angle-right"></a></div>',
            a[a.length] = "</div>", a[a.length] = '<div class="calendar-toolbar-rightbox"><span class="btnc totoday">今天</span></div >',
            a[a.length] = "</div>", $(a.join("")).appendTo(e.el);
    },
    _createCalendar: function () {
        var e = this, t = e.options.newDate, a = [];
        a[a.length] = '<div class="calendar">', e._createCalendarHeader(a), e._createCalendarMain(a, t),
            a[a.length] = "</div>", $(a.join("")).appendTo(e.el), e.holiDays.date = e._refreshCalendar(t);
    },
    _createCalendarHeader: function (e) {
        var t = this, a = t.options.calendar;
        e[e.length] = '<table cellspacing="0" cellpadding="0" style="width:100%">', e[e.length] = "<thead>",
            e[e.length] = '<tr class="calendar-header">', e[e.length] = ' <td class="calendar-weekend">' + a[0] + "</td>",
            e[e.length] = " <td>" + a[1] + "</td>", e[e.length] = " <td>" + a[2] + "</td>",
            e[e.length] = " <td>" + a[3] + "</td>", e[e.length] = " <td>" + a[4] + "</td>",
            e[e.length] = " <td>" + a[5] + "</td>", e[e.length] = ' <td class="calendar-weekend">' + a[6] + "</td>",
            e[e.length] = "</tr>", e[e.length] = "</thead>", e[e.length] = "</table>";
    },
    _createCalendarMain: function (e, t) {
        e[e.length] = '<div id="calendar-body">', e[e.length] = "</div>";
    },
    _refreshCalendar: function (e) {
        var t = [], a = this, n = a._cloneDate(e);
        n.getDate();
        n.setDate(1);
        var l = n.getDay(), o = a._cloneDate(n);
        o.setDate(o.getDate() - l - 1);
        var r = a.getHoliDay(e), s = 6;
        t[t.length] = '<table cellspacing="0" cellpadding="0" style="width:100%">';
        for (var d = 0; s > d; d++) {
            t[t.length] = "<tr>";
            for (var h = 0; 7 > h; h++) {
                t[t.length] = "<td>", o.setDate(o.getDate() + 1), o.getMonth() != e.getMonth() ? t[t.length] = '<div class="calendar-daynumber calendar-othermonth ' : o.getDate() == e.getDate() ? t[t.length] = '<div class="calendar-daynumber calendar-month  today ' : t[t.length] = '<div class="calendar-daynumber calendar-month  ',
                    Array.prototype.indexOf || (Array.prototype.indexOf = function (e) {
                        for (var t = this, a = 0; a < t.length; a++) if (t[a] == e) return a;
                        return -1;
                    }), r.indexOf(o.getTime()) > -1 ? (t[t.length] = 'calendar-weekend "', t[t.length] = ">") : t[t.length] = '">',
                    t[t.length] = '<span class="solar">' + o.getDate() + "</span>";
                var i = calendarChange.solar2lunar(o.getFullYear(), o.getMonth() + 1, o.getDate());
                i.isTerm === !0 ? t[t.length] = '<span class="lunar">' + i.Term + "</span >" : t[t.length] = '<span class="lunar">' + i.IDayCn + "</span >",
                    t[t.length] = "</div>", t[t.length] = "</td>";
            }
            t[t.length] = "</tr>";
        }
        return t[t.length] = '</table">', document.getElementById("calendar-body").innerHTML = t.join(""),
            r;
    },
    getMonthCount: function (e) {
        var e = new Date(e.getTime()), t = e.getMonth();
        return e.setMonth(t + 1), e.setDate(0), e.getDate();
    },
    getHoliDay: function (e) {
        var t = this;
        if (t.focusDays && t.focusDays.length > 0) return t.holiDays.date;
        var t = this, a = [], n = new Date(e.getFullYear(), e.getMonth(), e.getDate()), l = t.getMonthCount(n);
        n.setDate(1), n.setDate(n.getDate() - 1);
        for (var o = 0; l > o; o++) n.setDate(n.getDate() + 1), (0 == n.getDay() || 6 == n.getDay()) && a.push(t._cloneDate(n).getTime());
        return a;
    },
    _cloneDate: function (e) {
        return new Date(e.getFullYear(), e.getMonth(), e.getDate());
    },
    validate: function (e) { },
    createRange: function () {
        var e = this, t = e.options;
        e.startDate > e.endDate && (_date = e._cloneDate(e.startDate), e.startDate = e._cloneDate(e.endDate),
            e.endDate = e._cloneDate(_date));
        var a = (e._cloneDate(e.options.newDate), e.startDate.getDate()), n = e.endDate.getDate();
        $(".calendar-daynumber.calendar-month").each(function (e) {
            var t = $(this).find(".solar")[0].innerText;
            t >= a && n >= t && $(this).toggleClass("focus");
        }), $(".calendar-daynumber.focus").each(function () {
            var a = $(this).find(".solar")[0].innerText;
            1 == a.length && (a = 0 + a);
            var n = t.newDate.getMonth() + 1;
            n += "", 1 == n.length && (n = 0 + n), e.focusDays.push(t.newDate.getFullYear() + "-" + n + "-" + a);
        });
    },
    getHolidays: function () {
        for (var e = this, t = {}, a = [], n = 0; n < e.holiDays.date.length; n++) {
            var l = new Date(e.holiDays.date[n]);
            a.push(l.getFullYear() + "-" + (l.getMonth() + 1) + "-" + l.getDate());
        }
        return t.date = a, t;
    }
}, $.fn.calendar = function (e) {
    var t, a, n = "string" == typeof e;
    n && (t = $.makeArray(arguments), t.splice(0, 1));
    var l = "calendar", o = Calendar, r = this.each(function () {
        var r = $.data(this, l);
        r || (r = new o(this, e), $.data(this, l, r)), n && (a = r[e].apply(r, t));
    });
    return n ? a : r;
};